package com.cn.qgx428.orderingmeal;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.cn.qgx428.orderingmeal.myClass.MyKeyInfo;
import com.cn.qgx428.orderingmeal.myClass.User;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

public class Login extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "LoginActivity";

    private EditText account;
    private EditText password;

    private TextView signIn;
    private TextView login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        Bmob.initialize(this,"1b925fe2ccbb402f4034a3e28c31ad93");
        init();
        //设置EditText聚焦时无提示
        setEditTextFocusNotHint();
        //将缓存账号输入
        getUserInfo();
    }

    private void getUserInfo() {
        SharedPreferences sp = getSharedPreferences("userInfo",MODE_PRIVATE);
        account.setText(sp.getString("account",null));
        //光标移到行文本行末
        account.setSelection(account.getText().length());
        password.setText(sp.getString("password",null));
        password.setSelection(password.getText().length());
    }

    private void setEditTextFocusNotHint() {
        //getTag()  在view中存储一个格外的数据
        account.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                EditText _v=(EditText) view;
                if (!hasFocus) {// 失去焦点
                    _v.setHint(_v.getTag().toString());
                } else {
                    String hint=_v.getHint().toString();
                    _v.setTag(hint);
                    _v.setHint("");
                }
            }
        });

        password.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                EditText _v=(EditText) view;
                if (!hasFocus) {// 失去焦点
                    _v.setHint(_v.getTag().toString());
                } else {
                    String hint=_v.getHint().toString();
                    _v.setTag(hint);
                    _v.setHint("");
                }

            }
        });
    }

    private void init() {
        account = (EditText) findViewById(R.id.login_account);
        password =  (EditText) findViewById(R.id.login_password);
        signIn = (TextView) findViewById(R.id.login_signIn);
        signIn.setOnClickListener(this);
        login = (TextView) findViewById(R.id.login_login);
        login.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.login_signIn:
                startActivity(new Intent(Login.this, SignIn.class));
                break;
            case R.id.login_login:
                userLogin();
                break;
        }
    }

    private void userLogin() {
        User user = new User();
        user.setUsername(account.getText().toString());
        user.setPassword(password.getText().toString());
        user.login(new SaveListener<User>() {
            @Override
            public void done(User user, BmobException e) {
                if (e == null){
                    //将账号密码存入简单数据库
                    SharedPreferences sp = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
                    SharedPreferences.Editor editor = sp.edit();
                    editor.putString("account",account.getText().toString());
                    editor.putString("password",password.getText().toString());
                    editor.putString("isLogined","1");
                    editor.putString("userId",user.getObjectId());
                    editor.putString("userName",user.getName());
                    editor.commit();

                    //传递必要数据，启动页面
                    MyKeyInfo keyInfo = new MyKeyInfo();
                    keyInfo.setUserID(user.getObjectId());
                    keyInfo.setUserName(user.getName());
                    Intent intent = new Intent(Login.this, Home.class);
                    intent.putExtra("keyInfo",keyInfo);
                    startActivity(intent);
                    Log.d(TAG, "done: "+user.getObjectId());
                    finish();
                } else {
                    // 用户登陆错误提示
                    if (e.getErrorCode() == 101){
                        Toast.makeText(Login.this, "账号或密码错误", Toast.LENGTH_SHORT).show();
                    } else if(e.getErrorCode() == 9018){
                        Toast.makeText(Login.this, "账号或密码不能为空", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(Login.this, "未知错误", Toast.LENGTH_SHORT).show();
                        Log.d(TAG, "done: "+e);
                    }
                }
            }
        });
    }
}
